package cn.itsource.param.controller;


import cn.itsource.param.domain.Param;
import cn.itsource.param.service.ParamService;
import cn.itsource.system.query.ParamQuery;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {

    @Autowired
    private ParamService paramService;

    @GetMapping()
    public AjaxResult param(){
        return AjaxResult.ok(paramService.param());
    }


    /**
     * 分页查询
     * @param paramQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody ParamQuery paramQuery){
        try {
            return AjaxResult.ok(paramService.pageInfo(paramQuery));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 新增或修改
     * @param p
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Param p){
        paramService.save(p);
        return AjaxResult.ok();
    }


    /**
     * 批量删除
     * @param p
     * @return
     */
    @PatchMapping("/batchDeletion")
    public AjaxResult batchDeletion(@RequestBody List<Param> p){
        try {
            paramService.deleteAll(p);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            paramService.deleteByPrimaryKey(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
}
